import 'dart:convert';
import 'package:aitao_flutter/model/user_data.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import '../model/login_user_info.dart';
import '../tab/views/tab.dart';
import 'sp_keys.dart';

/// @class : SpUtil
/// @date : 2021/08/19
/// @name : jhf
/// @description :键值对存储
class SpUtil {
  /// 获取语言选项
  static Locale getLanguage() {
    String? language = GetStorage().read(SPKey.language);
    if (language == null) {
      return Get.deviceLocale!;
    }
    if (language == 'zh_CN') {
      return const Locale('zh', 'CN');
    } else if (language == 'zh_Hant') {
      return const Locale('zh', 'Hant');
    } else if (language == 'en_US') {
      return const Locale('en', 'US');
    }
    return const Locale('zh', 'CN');
  }

  /// 获取语言选项
  static String getLanguageStr() {
    String? language = GetStorage().read(SPKey.language);
    if (language == null) {
      return 'zh_CN';
    }
    return language;
  }

  // 存储语言选项
  static void saveLanguage(String language) {
    GetStorage().write(SPKey.language, language);
    // 刷新APP
    if (language == 'zh_CN') {
      Get.updateLocale(const Locale('zh', 'CN'));
    } else if (language == 'zh_Hant') {
      Get.updateLocale(const Locale('zh', 'Hant'));
    } else {
      Get.updateLocale(const Locale('en', 'US'));
    }
    Get.offAll(const Tabs());
  }

  /// 获取用户token
  static String getToken() {
    var token = GetStorage().read(SPKey.token);
    if (token == null) {
      return '';
    }
    return token;
  }

  // 存储用户token
  static void saveToken(String token) {
    GetStorage().write(SPKey.token, token);
  }

  /// 获取上次登录手机号码
  static String getLastLoginPhone() {
    var lastPhone = GetStorage().read(SPKey.lastPhone);
    if (lastPhone == null) {
      return '';
    }
    return lastPhone;
  }

  // 保存上次登录手机号码
  static void saveLastLoginPhone(String phone) {
    if (phone != '') {
      GetStorage().write(SPKey.lastPhone, phone);
    }
  }

  /// 获取用户IM_token
  static String getImToken() {
    var imToken = GetStorage().read(SPKey.imToken);
    if (imToken == null) {
      return '';
    }
    return imToken;
  }

  // 存储用户IM_token
  static void saveIMToken(String token) {
    GetStorage().write(SPKey.imToken, token);
  }

  /// 获取用户信息 -- 登录
  static LoginUserInfo getLoginUserInfo() {
    var userInfoStr = GetStorage().read(SPKey.userInfo);
    if (userInfoStr != null) {
      var jsonStr = json.decode(userInfoStr);
      LoginUserInfo userInfo = LoginUserInfo.fromJson(jsonStr);
      return userInfo;
    }
    return LoginUserInfo();
  }

  // 保存用户信息  -- 登录
  static void saveLoginUserInfo(LoginUserInfo userInfo) {
    var userJson = json.encode(userInfo);
    GetStorage().write(SPKey.userInfo, userJson);
  }

  /// 获取用户信息 -- 个人中心
  static UserData getUserData() {
    var userInfoStr = GetStorage().read(SPKey.userData);
    if (userInfoStr != null) {
      var jsonStr = json.decode(userInfoStr);
      UserData userData = UserData.fromJson(jsonStr);
      return userData;
    }
    return UserData();
  }

  // 保存用户信息  -- 个人中心
  static void saveUserData(UserData userData) {
    var userJson = json.encode(userData);
    GetStorage().write(SPKey.userData, userJson);
  }

  /// 清除用户信息,用于退出登录或登录失效
  static void cleanUserInfo() {
    GetStorage().remove(SPKey.userInfo);
    GetStorage().remove(SPKey.userData);
    GetStorage().remove(SPKey.token);
    GetStorage().remove(SPKey.imToken);
  }

  // 获取imageUrl
  static getImageUrl(String imageUrlSuffix) {
    var imageUrl = GetStorage().read(SPKey.imageServer);
    if (imageUrl == null) {
      return 'https://oss.xikaihuai.cn/$imageUrlSuffix';
    }
    return imageUrl + imageUrlSuffix;
  }

  /// 保存服务器配置
  static saveServerConfig(Map config) {
    // debugPrint('阿里云配置:$config');
    GetStorage().write(SPKey.imageServer, config['image_server']);
    GetStorage().write(SPKey.appUrl, config['appurl']);
    GetStorage().write(SPKey.ossEndpoint, config['oss_endpoint']);
    GetStorage().write(SPKey.ossBucket, config['oss_bucket']);
    GetStorage().write(SPKey.ossFilepath, config['oss_filepath']);
    GetStorage().write(SPKey.ossCallback, config['oss_callback']);
  }

  /// 保存App配置
  static saveAppConfig(Map config) {
    // debugPrint('App配置:$config');
    GetStorage().write(SPKey.sysId, config['sysId']);
    GetStorage().write(SPKey.sysName, config['sysName']);
    GetStorage().write(SPKey.sysAvatar, config['sysAvatar']);
    GetStorage().write(SPKey.servId, config['servId']);
    GetStorage().write(SPKey.servName, config['servName']);
    GetStorage().write(SPKey.servAvatar, config['servAvatar']);
  }

  /// 保存阿里云配置
  static saveOssConfig(Map config) {
    debugPrint('App配置:$config');
    GetStorage().write(SPKey.ossAccessKeyId, config['AccessKeyId']);
    GetStorage().write(SPKey.ossAccessKeySecret, config['AccessKeySecret']);
    GetStorage().write(SPKey.ossSecurityToken, config['SecurityToken']);
    GetStorage().write(SPKey.ossExpiration, config['Expiration']);
  }

  // 获取AccessKeyId
  static getAccessKeyId() {
    var ossAccessKeyId = GetStorage().read(SPKey.ossAccessKeyId);
    if (ossAccessKeyId == null) {
      return 'STS.NTwWc8mCHT2KJfPjzx2M7KxNw';
    }
    return ossAccessKeyId;
  }

  // 获取AccessKeySecret
  static getAccessKeySecret() {
    var ossAccessKeySecret = GetStorage().read(SPKey.ossAccessKeySecret);
    if (ossAccessKeySecret == null) {
      return '9u37PWoCRHfbTwdkz5DEZEuH2KH5byp1g2QeaSkKytWN';
    }
    return ossAccessKeySecret;
  }

  // 获取ossSecurityToken
  static getSecurityToken() {
    var ossSecurityToken = GetStorage().read(SPKey.ossSecurityToken);
    if (ossSecurityToken == null) {
      return 'CAISiwJ1q6Ft5B2yfSjIr5fCHNmMgJxp4/CgSEDhjnotPsIbpL3llTz2IHhMeHhhCOwavvs1mmBS6PwSlrh+W4NIX0rNaY5t9ZlN9wqkbtJEECVaOOVW5qe+EE2/VjTZvqaLEcibIfrZfvCyESOm8gZ43br9cxi7QlWhKufnoJV7b9MRLGLaBHg8c7UwHAZ5r9IAPnb8LOukNgWQ4lDdF011oAFx+wgdgOadupTAskGG3Qagm7FF/9+pcsmeApMybMslYbCcx/drc6fN6ilU5iVR+b1+5K4+om+f4oDMUgUNs0rcbbqOrIc/NnxwYqkrBqhDt+Pgkv51vOPekYntwgpKJ/tSVynPvTJjRBIvz4kagAFygrtCPA71MBbcJKJRtsXrP5+xA9znCQwkBBJDueWkHED2xaRn0mxn6vSdzwoylmXhbIvL4eMKSnuomsw+mxYwqZaX3ZHuasj7oPt6Kk3nMyAQqjk68bcOOsrJoBpvOz1/c0/KMDtc7qEovyzJrXyX5+G8ezmQNBgBb2Zm/LpQuSAA';
    }
    return ossSecurityToken;
  }

  // ossExpiration
  static getExpiration() {
    var ossExpiration = GetStorage().read(SPKey.ossExpiration);
    if (ossExpiration == null) {
      return '2023-11-03T08:28:16Z';
    }
    return ossExpiration;
  }

  // 获取appUrl
  static getAppUrl() {
    var appUrl = GetStorage().read(SPKey.appUrl);
    if (appUrl == null) {
      return 'https://api.aitaohy.com';
    }
    return appUrl;
  }

  // 获取imageServer
  static getImageServer() {
    var imageServer = GetStorage().read(SPKey.imageServer);
    if (imageServer == null) {
      return 'https://oss.idaoxiang.cn';
    }
    return imageServer;
  }

  // 获取ossEndpoint
  static getOssEndpoint() {
    var endpoint = GetStorage().read(SPKey.ossEndpoint);
    if (endpoint == null) {
      return 'https://oss-cn-hangzhou.aliyuncs.com';
    }
    return endpoint;
  }

  // 获取ossBucket
  static getOssBucket() {
    var bucket = GetStorage().read(SPKey.ossBucket);
    if (bucket == null) {
      return 'prod-aitao';
    }
    return bucket;
  }

  // 获取filepath
  static getOssFilepath() {
    var path = GetStorage().read(SPKey.ossFilepath);
    if (path == null) {
      return 'uploads/202310/28';
    }
    return path;
  }

  // 获取callback
  static getOssCallback() {
    var callback = GetStorage().read(SPKey.ossCallback);
    if (callback == null) {
      return 'https://api.aitaohy.com/a_oTDdC.php/oss/callback';
    }
    return callback;
  }

  /// 获取客服云信ID
  static String getServId() {
    var servId = GetStorage().read(SPKey.servId);
    if (servId == null) {
      return '4';
    }
    return servId;
  }

  // 存储客服云信ID
  static void saveServId(String servId) {
    GetStorage().write(SPKey.servId, servId);
  }

  /// 获取客服云信头像
  static String getServAvatar() {
    var servAvatar = GetStorage().read(SPKey.servAvatar);
    if (servAvatar == null) {
      return '/images/avatar/service.png';
    }
    return servAvatar;
  }

  // 存储客服云信头像
  static void saveServAvatar(String servAvatar) {
    GetStorage().write(SPKey.servAvatar, servAvatar);
  }

  /// 获取客服云信昵称
  static String getServName() {
    var servName = GetStorage().read(SPKey.servName);
    if (servName == null) {
      return '客服';
    }
    return servName;
  }

  // 存储客服云信昵称
  static void saveServName(String servName) {
    GetStorage().write(SPKey.servName, servName);
  }

  /// 获取系统云信ID
  static String getSysId() {
    var sysId = GetStorage().read(SPKey.sysId);
    if (sysId == null) {
      return '2';
    }
    return sysId;
  }

  // 存储系统云信ID
  static void saveSysId(String sysId) {
    GetStorage().write(SPKey.sysId, sysId);
  }

  /// 获取系统云信昵称
  static String getSysName() {
    var sysName = GetStorage().read(SPKey.sysName);
    if (sysName == null) {
      return '系统通知';
    }
    return sysName;
  }

  // 存储系统云信昵称
  static void saveSysName(String sysName) {
    GetStorage().write(SPKey.sysName, sysName);
  }

  /// 获取系统云信头像
  static String getSysAvatar() {
    var sysAvatar = GetStorage().read(SPKey.sysAvatar);
    if (sysAvatar == null) {
      return '/images/avatar/notice.png';
    }
    return sysAvatar;
  }

  // 存储系统云信头像
  static void saveSysAvatar(String sysAvatar) {
    GetStorage().write(SPKey.sysAvatar, sysAvatar);
  }

  /// 获取系统声音开关
  static bool getSystemVoiceStatus() {
    var openVoice = GetStorage().read(SPKey.openVoice);
    if (openVoice == null) {
      return true;
    }
    return openVoice;
  }

  // 存储系统声音开关
  static void saveSystemVoiceStatus(bool openVoice) {
    GetStorage().write(SPKey.openVoice, openVoice);
  }

  /// 获取系统振动开关
  static bool getSystemVibrationStatus() {
    var openVibration = GetStorage().read(SPKey.openVibration);
    if (openVibration == null) {
      return true;
    }
    return openVibration;
  }

  // 存储系统声音开关
  static void saveSystemVibrationStatus(bool openVibration) {
    GetStorage().write(SPKey.openVibration, openVibration);
  }

  /// 是否初次安装
  static bool getFirstLaunch() {
    var firstLaunch = GetStorage().read(SPKey.firstLaunch);
    if (firstLaunch == null) {
      return true;
    }
    return firstLaunch;
  }

  // 是否初次安装
  static void saveFirstLaunch(bool firstLaunch) {
    GetStorage().write(SPKey.firstLaunch, firstLaunch);
  }

  /// 青少年模式是否开启
  static bool getYoungModeStatus() {
    var openYoungMode = GetStorage().read(SPKey.openYoungMode);
    if (openYoungMode == null) {
      return false;
    }
    return openYoungMode;
  }

  // 青少年模式
  static void saveYoungModeStatus(bool openYoungMode) {
    GetStorage().write(SPKey.openYoungMode, openYoungMode);
  }

  /// IM消费是否设置了不再提醒
  static bool getPayIMNotTip() {
    var imNotTips = GetStorage().read(SPKey.imNotTips);
    if (imNotTips == null) {
      return false;
    }
    return imNotTips;
  }

  // IM消费是否设置不再提醒
  static void savePayIMNotTip(bool notTip) {
    GetStorage().write(SPKey.imNotTips, notTip);
  }

  /// App是否在前台
  static bool getAppLifeState() {
    var appLife = GetStorage().read(SPKey.appLife);
    if (appLife == null) {
      return false;
    }
    return appLife;
  }

  // App是否在前台
  static void saveAppLifeState(bool appLife) {
    GetStorage().write(SPKey.appLife, appLife);
  }

  /// 是否正在视频
  static bool getIsVideo() {
    var isVideo = GetStorage().read(SPKey.isVideo);
    if (isVideo == null) {
      return false;
    }
    return isVideo;
  }

  // 是否正在视频
  static void saveIsVideo(bool isVideo) {
    GetStorage().write(SPKey.isVideo, isVideo);
  }
}
